﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public partial class Tool_System_StoreHouse_Form : DevExpress.XtraEditors.XtraForm
    {
        private class ItemValue
        {
            public string sToolName = "";
            public int iNormal = 0;
            public int iBorrow = 0;
            public int iDamage = 0;
            public int iLose = 0;
        }

        private int m_iPosID = 0;
        private string m_sPosName = "";

        public Tool_System_StoreHouse_Form()
        {
            InitializeComponent();
        }

        private void Tool_System_StoreHouse_Form_Load(object sender, EventArgs e)
        {
            LoadButton();
        }

        private void LoadButton()
        {
            flp_store.Controls.Clear();

            DataTable oButtonDT = ToolDataMgr.GetInstance().GetToolPosChildren(0);
            for (int i = 0; i < oButtonDT.Rows.Count; i++)
            {
                int iPositionID = int.Parse(oButtonDT.Rows[i]["f_id"].ToString());

                CheckButton oBtn = new CheckButton();
                oBtn.AutoWidthInLayoutControl = true;
                oBtn.Height = 35;
                oBtn.Width = flp_store.Width - flp_store.Padding.Left - flp_store.Padding.Right;
                oBtn.Text = oButtonDT.Rows[i]["f_name"].ToString();
                oBtn.GroupIndex = 0;
                oBtn.Tag = iPositionID;
                oBtn.Click += new EventHandler(btn_Click);

                if (i == 0)
                {
                    oBtn.Checked = true;
                    oBtn.PerformClick();
                }

                flp_store.Controls.Add(oBtn);
            }
        }

        private void LoadChildrenData(int iParentID)
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("f_id");
            dataTable.Columns.Add("f_name");
            dataTable.Columns.Add("f_toolname");
            dataTable.Columns.Add("f_normal");
            dataTable.Columns.Add("f_borrow");
            dataTable.Columns.Add("f_damage");
            dataTable.Columns.Add("f_lose");
            dataTable.Columns.Add("f_total");

            DataTable oChildren = ToolDataMgr.GetInstance().GetToolPosChildren(iParentID);
            for (int i = 0; i < oChildren.Rows.Count; i++)
            {
                int iPositionID = int.Parse(oChildren.Rows[i]["f_id"].ToString());
                string sName = oChildren.Rows[i]["f_name"].ToString();

                DataTable oToolDT = ToolDataMgr.GetInstance().GetToolByPos(iPositionID);
                if (oToolDT.Rows.Count > 0)
                {
                    Dictionary<int, ItemValue> oDic = new Dictionary<int, ItemValue>();

                    for (int j = 0; j < oToolDT.Rows.Count; j++)
                    {
                        DataRow rowTool = oToolDT.Rows[j];
                        int iToolTypeID = int.Parse(rowTool["f_typeid"].ToString());
                        if (!oDic.ContainsKey(iToolTypeID))
                        {
                            oDic.Add(iToolTypeID, new ItemValue());
                        }
                        
                        oDic[iToolTypeID].sToolName = rowTool["f_tooltypename"].ToString();
                        int iStatus = int.Parse(rowTool["f_statetype"].ToString());
                        if (iStatus == 0)
                        {
                            oDic[iToolTypeID].iNormal++;
                        }
                        else if (iStatus == 1)
                        {
                            oDic[iToolTypeID].iBorrow++;
                        }
                        else if (iStatus == 2)
                        {
                            oDic[iToolTypeID].iDamage++;
                        }
                        else if (iStatus == 3)
                        {
                            oDic[iToolTypeID].iLose++;
                        }
                    }

                    foreach (ItemValue value in oDic.Values)
                    {
                        DataRow rowNew = dataTable.NewRow();

                        rowNew["f_id"] = iPositionID;
                        rowNew["f_name"] = sName;
                        rowNew["f_toolname"] = value.sToolName;
                        rowNew["f_normal"] = value.iNormal;
                        rowNew["f_borrow"] = value.iBorrow;
                        rowNew["f_damage"] = value.iDamage;
                        rowNew["f_lose"] = value.iLose;
                        dataTable.Rows.Add(rowNew);
                    }
                }
                else
                {
                    DataRow rowNew = dataTable.NewRow();
                    rowNew["f_id"] = iPositionID;
                    rowNew["f_name"] = sName;
                    dataTable.Rows.Add(rowNew);
                }
            }
            
            gc_Main.DataSource = dataTable;
            gv_view.ExpandAllGroups();
        }

        private void btn_Click(object sender, System.EventArgs e)
        {
            CheckButton oButton = (CheckButton)sender;
            LoadChildrenData((int)oButton.Tag);
        }

        private void gv_view_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
        {
            GridGroupRowInfo gridGroupRowInfo = e.Info as GridGroupRowInfo;
            GridView gridview = sender as GridView;
            int index = gridview.GetDataRowHandleByGroupRowHandle(e.RowHandle);

            gridGroupRowInfo.GroupText = gridview.GetRowCellValue(index, "f_name").ToString();
        }

        private void bbi_addtop_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Tool_System_ToolPosEdit_Form oForm = new Tool_System_ToolPosEdit_Form();

            if (oForm.ShowDialog() == DialogResult.OK)
            {
                LoadButton();
            }
        }

        private void bbi_modifytop_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CheckButton oButton = GetCheckButton();
            if (oButton != null)
            {
                Tool_System_ToolPosEdit_Form oForm = new Tool_System_ToolPosEdit_Form();
                oForm.OToolPos.IID = (int)oButton.Tag;
                oForm.OToolPos.SName = oButton.Text;

                if (oForm.ShowDialog() == DialogResult.OK)
                {
                    LoadButton();
                }
            }
        }

        private void bbi_deletetop_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CheckButton oButton = GetCheckButton();
            if (oButton != null)
            {
                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                String strMsg = "确定要删除库房:" + oButton.Text + "？";
                DialogResult dr = MessageBox.Show(strMsg, "删除工具", messButton);
                if (dr == DialogResult.OK)//如果点击“确定”按钮
                {
                    if (ToolDataMgr.GetInstance().DeleteToolPos((int)oButton.Tag))
                    {
                        LoadButton();
                    }
                }
            }
        }

        private void bbi_addpos_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CheckButton oButton = GetCheckButton();
            if (oButton != null)
            {
                Tool_System_ToolPosEdit_Form oForm = new Tool_System_ToolPosEdit_Form();
                oForm.OToolPos.IParentID = (int)oButton.Tag;
                oForm.OToolPos.SParentName = oButton.Text;

                if (oForm.ShowDialog() == DialogResult.OK)
                {
                    LoadButton();
                }
            }
        }

        private void bbi_modifypos_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CheckButton oButton = GetCheckButton();
            if (oButton != null)
            {
                Tool_System_ToolPosEdit_Form oForm = new Tool_System_ToolPosEdit_Form();
                oForm.OToolPos.IParentID = (int)oButton.Tag;
                oForm.OToolPos.SParentName = oButton.Text;
                oForm.OToolPos.IID = m_iPosID;
                oForm.OToolPos.SName = m_sPosName;

                if (oForm.ShowDialog() == DialogResult.OK)
                {
                    LoadButton();
                }
            }
        }

        private void bbi_deletepos_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CheckButton oButton = GetCheckButton();
            if (oButton != null)
            {
                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                String strMsg = "确定要删除库房:" + m_sPosName + "？";
                DialogResult dr = MessageBox.Show(strMsg, "删除工具", messButton);
                if (dr == DialogResult.OK)//如果点击“确定”按钮
                {
                    if (ToolDataMgr.GetInstance().DeleteToolPos(m_iPosID))
                    {
                        LoadButton();
                    }
                }
            }
        }

        // 获取当前checkbutton
        private CheckButton GetCheckButton()
        {
            foreach (Control oCtrl in flp_store.Controls)
            {
                CheckButton oButton = oCtrl as CheckButton;
                if (oButton.Checked)
                {
                    return oButton;
                }
            }

            return null;
        }

        private void gv_view_GotFocus(object sender, EventArgs e)
        {
            bbi_modifypos.Enabled = true;
            bbi_deletepos.Enabled = true;
        }

        private void gv_view_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow dataRow = gv_view.GetDataRow(e.FocusedRowHandle);
            if (dataRow != null)
            {
                m_iPosID = int.Parse(dataRow[0].ToString());
                m_sPosName = dataRow[1].ToString();
            }
        }

        private void gv_view_LostFocus(object sender, EventArgs e)
        {
            bbi_modifypos.Enabled = false;
            bbi_deletepos.Enabled = false;
        }
    }
}